<?php
    $domain             = 'goindiebooks.com';
    $wp_install_dir     = site_url();
    $theme_src          = '/devcentral/repository/wp-themes/natural/src/natural-patched';
    $doc_root           = get_root_relative_path($wp_install_dir, $domain);

    $copy_src           = $doc_root . $theme_src;
    $copy_dest          = '../wp-content/themes/natural';

    $is_natural_already_installed = doesThisDirectoryExist($copy_dest);


    function get_root_relative_path($path, $domain) {
        $search_index = strpos($path, $domain);
        $cleaned_path = substr($path, $search_index);

        $rel_path  = preg_replace('/[^\/]*\//i', '../', $cleaned_path);
        $rel_path  = preg_replace('/\/[^\/]*$/i', '/..', $rel_path);

        return $rel_path;
    }

    function doesThisDirectoryExist($dir) {
        return (file_exists($dir) && is_dir($dir));
    }

    clearstatcache();
?>

<script>
    var Bridge = {
        wp_ajax_url : "<?php echo admin_url('admin-ajax.php'); ?>",
        copy_src    : "<?php echo $copy_src; ?>",
        copy_dest   : "<?php echo $copy_dest; ?>"
    };
</script>


        <div id="bridge" class="wrap">
            <div class="icon32 bridge-icon"></div>
            <h2>Natural Theme Bridge Installer</h2>

            <br>
            <br>

            <div id="messages">
<?php
                if ($is_natural_already_installed) {
?>
                    <h1><span class="warning">Warning!</span></h1>
                    <p>The directory "natural" already existed on your themes directory. You might have already installed "Natural Theme" in the past. Please go to <a href="themes.php" class="path">Appearance > Themes</a> and check if this statement is true.</p>
<?php
                } else {
                    if (doesThisDirectoryExist($copy_src)) {
?>
                        <h1>Install it now</h1>
                        <p>You can install Natural Theme by clicking the button below.</p>
                        <p>This installation includes theme patches.</p>
                        <br>
                        <input type="button" value="Install" id="bridge-install" class="button-primary">
                        <script type="text/javascript" src="<?php echo OF_DIRECTORY; ?>/js/bridge-install.js"></script>
<?php
                    } else {
?>
                        <h1><span class="error">Ooops!!!</span></h1>
                        <p>Sorry, I can't find the theme on this location <span class="path"><?php echo $copy_src; ?></span>. Make sure your are at <span class="path"><?php echo $domain; ?></span></p>

<?php
                    }
                }
?>
            </div>
        </div>